<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Repositories\GameRoomRepository;
use App\Http\Requests\Admin\CreateGameRoomRequest;
use App\Http\Requests\Admin\EditGameRoomRequest;

class GameRoomController extends Controller
{
    protected $Repo;

    public function __construct(GameRoomRepository $repo)
    {
        $this->middleware('admin.auth');
        $this->Repo = $repo;
    }

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $rooms = $this->Repo->getAllRooms();

        return view('admin.gameRooms.index', compact('rooms'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('admin.gameRooms.create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(CreateGameRoomRequest $request)
    {
        $result = $this->Repo->addRoom($request);

        if (is_object($result)) {
            $data = [
                'status' => 0,
                'msg'    => '房间添加成功',
                'url'    => route('gamerooms.index')
            ];
        } else {
            $data = [
                'status' => 2,
                'msg'    => '房间添加失败'
            ];
        }
        return json_encode($data);
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $room = $this->Repo->getRoom($id);

        return view('admin.gameRooms.edit', compact('room'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(EditGameRoomRequest $request, $id)
    {
        $res = $this->Repo->updateRoom($request, $id);

        if ($res) {
            $data = [
                'status' => 0,
                'msg'    => '房间更新成功',
                'url'    => route('gamerooms.index')
            ];
        } else {
            $data = ['status' => 2, 'msg' => '房间更新失败'];
        }

        return json_encode($data);
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $res = $this->Repo->deleteRoom($id);
        if ($res) {
            $data = [
                'status' => 0,
                'msg'    => '房间删除成功',
                'url'    => route('gamerooms.index')
            ];
        } else {
            $data = ['status' => 2, 'msg' => '房间删除失败'];
        }
        return json_encode($data);
    }

    //批量删除
    public function destroyList(Request $request)
    {
        $ids = explode(',', $request->ids);

        if ($request->isMethod('post')) {
            $res = $this->Repo->deleteRoomsLists($ids);
            if ($res) {
                $data = [
                    'status' => 0,
                    'msg'    => '房间删除成功',
                    'url'    => route('gamerooms.index')
                ];
            } else {
                $data = ['status' => 2, 'msg' => '房间删除失败'];
            }
            return json_encode($data);
        }
    }

    public function editStatus(Request $request, $id)
    {

        $res = $this->Repo->editStatus($id, $request->status);
        if ($res) {
            $data = [
                'status' => 0,
                'msg' => '操作成功',
            ];
        } else {
            $data = ['status' => 3, 'msg' => '操作失败'];
        }
        return json_encode($data);
    }
}
